package com.joymeter.data.entity;

import com.alibaba.fastjson.JSONObject;
import com.joymeter.common.utils.EmptyUtils;

/**
 * @ClassName EventInfo
 * @Description TODO
 * 网关实时发送的数据格式
 * @Author liang
 * @Date 2018/8/15 11:33
 * @Version 1.0
 **/
public class EventInfo {
    private String serverId;//服务端编号
    private String deviceId;//设备编号
    /**
     * 0 未知;1 服务器;10 CPU;11 Memory;12 Disk;13 Network;14 应用服务心跳;2 基站/母表;2100 基站;2200 母表;2300 采集器;3 终端设备;31 电表;3100 JLAA无线电表;3101 有线电表;3102 GPRS645电表;3103 JLAA无线智能插座;32 冷水表;3200 JLAA无线冷水表;3201 有线冷水表;33 	热水表;3300 JLAA无线热水表;3301 有线热水表;34 气表;3400 JLAA无线气表;3401 有线气表;35 热表;3500 JLAA无线热表;3501 有线热表
     */
    private String type;//设备类型
    /**
     * data 数据;data_failed 读表失败;callback_failed 回调失败;online 上线;offline 离线;keepalive 心跳;open 开的状态;open_failed 开失败;close 关的状态;close_failed 关失败;error 故障设备;unknown 未知设备
     */
    private String event;//设备类型
    private String data;//数据
    //private String msg;//事件描述-->Druid数据库中为eventInfo
    private String eventInfo;
    private String datetime;//时间

    public EventInfo() {
    }

    public EventInfo(String data){
        super();
        JSONObject jsonObject = JSONObject.parseObject(data);
        this.serverId=jsonObject.getString("serverId");
        this.deviceId=jsonObject.getString("deviceId");
        this.type=jsonObject.getString("type");
        this.data=jsonObject.getString("data");
        this.event=jsonObject.getString("event");

        String msg = jsonObject.getString("msg");
        String eventInfo = jsonObject.getString("eventInfo");
        if(EmptyUtils.isEmpty(msg) && !EmptyUtils.isEmpty(eventInfo)){
            this.eventInfo=eventInfo;
        }else {
            this.eventInfo=msg;
        }
        this.datetime=jsonObject.getString("datetime");
    }


    /**
     * 非空校验
     * @return
     */
    public boolean isEmpty(){
        if (EmptyUtils.isEmpty(serverId) || EmptyUtils.isEmpty(deviceId) || EmptyUtils.isEmpty(type) || EmptyUtils.isEmpty(event)
                || EmptyUtils.isEmpty(datetime)?true:Long.valueOf(datetime) <= 0){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 转成json格式
     * @return
     */
    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("serverId",serverId);
        jsonObject.put("deviceId",deviceId);
        jsonObject.put("type",type);
        jsonObject.put("event",event);
        jsonObject.put("data",data);
        jsonObject.put("eventInfo",eventInfo);
        jsonObject.put("datetime",datetime);
        return jsonObject.toString();
    }

    public String getServerId() {
        return serverId;
    }

    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getEventInfo() {
        return eventInfo;
    }

    public void setEventInfo(String eventInfo) {
        this.eventInfo = eventInfo;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }
}
